home *** CD-ROM | disk | FTP | other *** search
- /***
- *** Copyright (c) 1990 Rijksinstituut voor Visserijonderzoek, IJmuiden.
- *** All rights reserved.
- ***
- *** Redistribution and use in source and binary forms are permitted
- *** provided that this notice is preserved and that due credit is given
- *** to the Rijksinstituut voor Visserijonderzoek at IJmuiden.
- *** This software is provided "as is" without express or implied warranty.
- ***
- *** @(#)MacSocket.c 1.7 91/04/04
- ***/
-
- /*
- #define MAC_APPL
- */
- #include "MacSocket.h"
-
- #define Connected (0x01)
-
- typedef struct SockElmnt
- {
- int number;
- struct SockElmnt *next;
- int state;
- TCPiopb thePB;
- char sockBuf[SockBufSize];
- } SockElmnt, *SockElmntPtr, **SockElmntHdl;
-
- #ifdef MAC_APPL
-
- #define LineLength (350)
- #define AMargin (13)
- #define BMargin (23)
- #define IconSize (32)
- #define DlogTop (66)
- #define DlogLeft (50)
- #define ButtonHeight (22)
- #define ButtonLength (59)
-
- typedef struct AlertDitl
- {
- unsigned short nItems;
- Handle textHandle;
- Rect textRect;
- unsigned char text;
- unsigned char textSize;
- char textString[8];
- Handle buttonHandle;
- Rect buttonRect;
- unsigned char button;
- unsigned char buttonSize;
- char buttonString[2];
- Handle iconHandle;
- Rect iconRect;
- unsigned char icon;
- unsigned char iconSize;
- unsigned short iconId;
- Handle outlineHandle;
- Rect outlineRect;
- unsigned char outline;
- unsigned char outlineSize;
- } AlertDitl, *AlertDitlPtr, **AlertDitlHdl;
-
- #endif MAC_APPL
-
- #ifdef close
- #undef close
- #endif close
-
- #ifdef exit
- #undef exit
- #ifdef MAC_APPL
- #define exit(s) ExitToShell()
- #endif MAC_APPL
- #endif exit
-
- #ifdef perror
- #undef perror
- #endif perror
-
- #ifdef strerror
- #undef strerror
- #endif strerror
-
- /**
- **
- ** local prototypes
- **
- **/
- static void DoAlert(int alertId, char* s1, char* s2, char* s3, char* s4);
- static SockElmntPtr CreateSocketStruct(void);
- static SockElmntPtr FindSocketStruct(int);
- static int OpenTCPDriver(void);
- static pascal void OutlineProc(DialogPtr thePtr, int theItem);
- static void PollTCPDriver(TCPiopb *thePB);
- static int RemoveSocketStruct(int);
- static pascal void SockNotify(StreamPtr tcpStream,
- unsigned short eventCode,
- Ptr userDataPtr,
- unsigned short terminReason,
- struct ICMPReport *icmpMsg);
-
-
- /**
- **
- ** local variables
- **
- **/
- static int errno_MacSocket = noErr;
- static int ioRefNumTCP;
- static char macSocketErrorMssg[128] = "no error!";
- static int showTime = 4;
- static int sockNumber = 4;
- static SockElmntPtr theHeadPtr = NIL;
- static char TCPDriverName[] = "\p.IPP";
- static char theUserData[UserDataSize];
- static char TCPDriverNotOpened = True;
-
- #ifdef LAB_VIEW
-
- static SockElmnt theSockElmnt;
-
- #endif LAB_VIEW
-
- /***
- ***
- *** Mac versions the standard Socket-based internet communication protocol:
- ***
- ***/
-
- /**
- **
- ** Local functions
- **
- **/
-
- /*
- *
- * CreateSocketStruct - creates a data structure for a particular socket into a
- * linked list
- *
- */
- static SockElmntPtr CreateSocketStruct()
- {
- register SockElmntPtr theSockPtr;
-
- if ((theSockPtr = (SockElmntPtr)malloc(SizeOf(SockElmnt))) == NIL)
- {
- errno_MacSocket = noBufSpace;
- return (NIL);
- }
- theSockPtr->next = theHeadPtr;
- theSockPtr->state = 0x00;
- theHeadPtr = theSockPtr;
- theSockPtr->number = ++sockNumber;
- return (theSockPtr);
- }
-
- #ifdef MAC_APPL
- /*
- *
- * DoAlert - shows an alert message on the screen. When alertId is negative,
- * no icon and OK-button are displayed. The Alert message is then "showTime"
- * seconds visible.
- *
- */
- static void DoAlert(int alertId, char* s1, char* s2, char* s3, char* s4)
- {
- AlertDitlPtr theAlertDitlPtr;
- AlertDitlHdl theAlertDitlHdl;
- DialogRecord dlogRecord;
- DialogPtr theDlogPtr;
- Rect theBounds;
- int theItem;
- int textHeight;
- int nChars;
- FontInfo theFont;
-
- if ((alertId < 0) && (showTime <= 0))
- {
- return;
- }
-
- theAlertDitlHdl = (AlertDitlHdl)NewHandle(sizeof(AlertDitl));
- if (theAlertDitlHdl != NIL)
- {
- HLock(theAlertDitlHdl);
- theAlertDitlPtr = * theAlertDitlHdl;
-
- nChars = (int)((unsigned char)*s1) +
- (int)((unsigned char)*s2) +
- (int)((unsigned char)*s3) +
- (int)((unsigned char)*s4);
-
- GetFontInfo(&theFont);
- textHeight = ((nChars * theFont.widMax) / LineLength + 1) *
- (theFont.ascent + theFont.descent + theFont.leading);
- if ((textHeight < IconSize) && (alertId >= 0))
- {
- textHeight = IconSize;
- }
-
- theAlertDitlPtr->nItems = (alertId >= 0) ? 3 : 0;
-
- theAlertDitlPtr->textHandle = ((void*) 0L);
- theAlertDitlPtr->textRect.top = AMargin - 3;
- theAlertDitlPtr->textRect.left = BMargin - 3;
- if (alertId >= 0)
- {
- theAlertDitlPtr->textRect.left += IconSize + BMargin;
- }
- theAlertDitlPtr->textRect.bottom =
- theAlertDitlPtr->textRect.top + textHeight;
- theAlertDitlPtr->textRect.right =
- theAlertDitlPtr->textRect.left + LineLength;
- theAlertDitlPtr->text = statText + itemDisable;
- theAlertDitlPtr->textSize = 8;
- theAlertDitlPtr->textString[0] = '^';
- theAlertDitlPtr->textString[1] = '0';
- theAlertDitlPtr->textString[2] = '^';
- theAlertDitlPtr->textString[3] = '1';
- theAlertDitlPtr->textString[4] = '^';
- theAlertDitlPtr->textString[5] = '2';
- theAlertDitlPtr->textString[6] = '^';
- theAlertDitlPtr->textString[7] = '3';
-
- if (alertId >= 0)
- {
- theAlertDitlPtr->buttonHandle = ((void*) 0L);
- theAlertDitlPtr->buttonRect.top =
- theAlertDitlPtr->textRect.bottom + AMargin;
- theAlertDitlPtr->buttonRect.right =
- theAlertDitlPtr->textRect.right;
- theAlertDitlPtr->buttonRect.bottom =
- theAlertDitlPtr->buttonRect.top + ButtonHeight;
- theAlertDitlPtr->buttonRect.left =
- theAlertDitlPtr->buttonRect.right - ButtonLength;
- theAlertDitlPtr->button = ctrlItem + btnCtrl;
- theAlertDitlPtr->buttonSize = 2;
- theAlertDitlPtr->buttonString[0] = 'O';
- theAlertDitlPtr->buttonString[1] = 'K';
-
- theAlertDitlPtr->iconHandle = ((void*) 0L);
- theAlertDitlPtr->iconRect.top = theAlertDitlPtr->textRect.top;
- theAlertDitlPtr->iconRect.left = BMargin - 3;
- theAlertDitlPtr->iconRect.bottom =
- theAlertDitlPtr->iconRect.top + IconSize;
- theAlertDitlPtr->iconRect.right =
- theAlertDitlPtr->iconRect.left + IconSize;
- theAlertDitlPtr->icon = iconItem + itemDisable;
- theAlertDitlPtr->iconSize = 2;
- theAlertDitlPtr->iconId = alertId;
-
- theAlertDitlPtr->outlineHandle = (Handle)&OutlineProc;
- theAlertDitlPtr->outlineRect.top = theAlertDitlPtr->buttonRect.top;
- theAlertDitlPtr->outlineRect.left = theAlertDitlPtr->buttonRect.left;
- theAlertDitlPtr->outlineRect.bottom = theAlertDitlPtr->buttonRect.bottom;
- theAlertDitlPtr->outlineRect.right = theAlertDitlPtr->buttonRect.right;
- theAlertDitlPtr->outline = userItem + itemDisable;
- theAlertDitlPtr->outlineSize = 0;
- }
-
- theBounds.top = DlogTop;
- theBounds.left = DlogLeft;
- theBounds.bottom = DlogTop + AMargin - 3;
- theBounds.bottom += (alertId >= 0) ?
- theAlertDitlPtr->buttonRect.bottom :
- theAlertDitlPtr->textRect.bottom;
- theBounds.right = DlogLeft + theAlertDitlPtr->textRect.right + AMargin - 3;
-
- HUnlock(theAlertDitlHdl);
-
- ParamText(s1, s2, s3, s4);
- theDlogPtr = NewDialog(&dlogRecord, &theBounds, "\p", True, dBoxProc,
- InFrontOfAll, False, 0L, theAlertDitlHdl);
- if (alertId >= 0)
- {
- ModalDialog(NIL, &theItem);
- }
- else
- {
- DrawDialog(theDlogPtr);
- sleep(showTime);
- }
- CloseDialog(theDlogPtr);
- DisposHandle(theAlertDitlHdl);
- }
- };
- #endif MAC_APPL
-
- /*
- *
- * FindSocketStruct - finds a data structure for a particular socket in a linked list
- *
- */
- static SockElmntPtr FindSocketStruct(int sock)
- {
- register SockElmntPtr theSockPtr;
-
- for (theSockPtr = theHeadPtr; theSockPtr != NIL;
- theSockPtr = (SockElmntPtr)theSockPtr->next)
- {
- if (theSockPtr->number == sock)
- {
- errno_MacSocket = noErr;
- return (theSockPtr);
- }
- }
- errno_MacSocket = nonSocket;
- return (NIL);
- }
-
- /*
- *
- * OpenTCPDriver - loads and opens UDP-TCP/IP driver (".IPP")
- *
- */
- static int OpenTCPDriver()
- {
- ParamBlockRec thePB;
-
- thePB.ioParam.ioCompletion = NIL;
- thePB.ioParam.ioNamePtr = (StringPtr)TCPDriverName;
- thePB.ioParam.ioPermssn = fsCurPerm;
- PBOpen(&thePB, Asynchronous);
- PollTCPDriver((TCPiopb *)&thePB);
- if (errno_MacSocket == noErr)
- {
- TCPDriverNotOpened = False;
- ioRefNumTCP = thePB.ioParam.ioRefNum;
- return(1);
- }
- else
- {
- errno_MacSocket = noTCPDriver;
- return(0);
- }
- }
-
- #ifdef MAC_APPL
- /*
- *
- * OutlineProc - Outlines an item in a dialogbox
- *
- */
- static pascal void OutlineProc(WindowPtr thePtr, int theItem)
- {
- int theItemType;
- GrafPtr currentPort;
- Handle theItemHdl;
- Rect theBox;
-
- GetDItem (thePtr, theItem, &theItemType, &theItemHdl, &theBox);
- GetPort(¤tPort);
- SetPort(thePtr);
- PenSize(3, 3);
- InsetRect(&theBox, -4, -4);
- FrameRoundRect (&theBox, 16, 16);
- PenNormal();
- SetPort(currentPort);
- };
- #endif MAC_APPL
-
- /*
- *
- * PollTCPDriver - polls the UDP-TCP/IP driver until an action is completed
- *
- */
- static void PollTCPDriver(TCPiopb *theTCPPtr)
- {
- while((errno_MacSocket = theTCPPtr->ioResult) > 0)
- {
- SystemTask();
- }
- }
-
- /*
- *
- * RemoveSocketStruct - removes a data structure for a particular socket from a
- * linked list
- *
- */
- static int RemoveSocketStruct(int sock)
- {
- register SockElmntPtr theSockPtr, thePrevOne = NIL;
-
- for (theSockPtr = theHeadPtr; theSockPtr != NIL;
- thePrevOne = theSockPtr, theSockPtr = (SockElmntPtr)theSockPtr->next)
- {
- if (theSockPtr->number == sock)
- {
- if (thePrevOne == NIL)
- {
- theHeadPtr = theSockPtr->next;
- }
- else
- {
- thePrevOne->next = theSockPtr->next;
- }
- free(theSockPtr);
- errno_MacSocket = noErr;
- return (1);
- }
- }
- errno_MacSocket = nonSocket;
- return(0);
- }
-
- /*
- *
- * SockNotify - prints notification messages for the UDP-TCP/IP driver
- *
- */
- static pascal void SockNotify(StreamPtr tcpStream, unsigned short eventCode,
- Ptr userDataPtr, unsigned short terminReason, struct ICMPReport *icmpMsg)
- {
- char theErrorMsg[128];
- char *errStrPtr1 = "\0", *errStrPtr2 = "\0";
- #ifdef MAC_APPL
- char errNoStr[32];
- #endif MAC_APPL
-
- strcpy(theErrorMsg, "MacSocket: ");
- switch (eventCode)
- {
- case TCPClosing:
- errStrPtr1 = "all data has been received and delivered";
- break;
- case TCPULPTimeout:
- errStrPtr1 = "time out";
- break;
- case TCPTerminate:
- errStrPtr1 = "connection terminated, ";
- switch(terminReason)
- {
- case TCPRemoteAbort:
- errStrPtr2 = "connection reset by peer";
- break;
- case TCPNetworkFailure:
- errStrPtr2 = "network failure";
- break;
- case TCPSecPrecMismatch:
- errStrPtr2 = "invallid security option or precedence level";
- break;
- case TCPULPTimeoutTerminate:
- errStrPtr2 = "ULP time out";
- break;
- case TCPULPAbort:
- errStrPtr2 = "connection aborted";
- break;
- case TCPULPClose:
- errStrPtr2 = "connection closed gracefully";
- break;
- case TCPServiceError:
- errStrPtr2 = "unexpected connection initiation segment read";
- break;
- default:
- errStrPtr2 = "unknown reason";
- break;
- }
- break;
- case TCPDataArrival:
- errStrPtr1 = "data arrived, no receive outstanding";
- break;
- case TCPUrgent:
- errStrPtr1 = "urgent data arrived";
- break;
- case TCPICMPReceived:
- errStrPtr1 = "Internet Control Message arrived";
- /* still to be printed */
- break;
- default:
- errStrPtr1 = "unknown eventcode";
- break;
- }
- strcat(theErrorMsg, errStrPtr1);
- strcat(theErrorMsg, errStrPtr2);
- #ifndef MAC_APPL
- fprintf(stderr, "%s (%d).\n", theErrorMsg, eventCode);
- #else MAC_APPL
- NumToString(eventCode, errNoStr);
- DoAlert(-1, CtoPstr(theErrorMsg), "\p(", errNoStr, "\p)");
- #endif MAC_APPL
- };
-
- /**
- **
- ** Global functions
- **
- **/
-
- /**
- **
- ** Functions that substitute relevant UNIX-system calls on the Mac
- **
- **/
-
- /*
- *
- * close_MacSocket - delete a (socket) descriptor if present, else do close(int s)
- *
- */
- int close_MacSocket(int s)
- {
- TCPiopb *theTCPPtr;
- SockElmntPtr theSockPtr, thePrevPtr = NIL;
-
- if ((theSockPtr = FindSocketStruct(s)) == NIL)
- {
- return(close(s));
- }
-
- theTCPPtr = &(theSockPtr->thePB);
- theTCPPtr->csCode = TCPClose;
- theTCPPtr->csParam.close.ulpTimeoutValue = CloseTimeout;
- theTCPPtr->csParam.close.ulpTimeoutAction = Abort;
- theTCPPtr->csParam.close.validityFlags = (byte)(timeoutValue | timeoutAction);
- theTCPPtr->csParam.close.userDataPtr = (Ptr)&theUserData;
- PBControl((ParmBlkPtr)theTCPPtr, Asynchronous);
- PollTCPDriver(theTCPPtr);
- theSockPtr->number = -1;
- if (errno_MacSocket == noErr)
- {
- return(0);
- }
- else
- {
- return (-1);
- }
- };
-
- /*
- *
- * exit_MacSocket - substitute for exit(int status)
- *
- */
- void exit_MacSocket(int status)
- {
- ParamBlockRec thePB;
-
- while (theHeadPtr != NIL)
- {
- shutdown(theHeadPtr->number, 2);
- }
-
- if (!TCPDriverNotOpened)
- {
- thePB.ioParam.ioCompletion = NIL;
- thePB.ioParam.ioRefNum = ioRefNumTCP;
- PBClose(&thePB, Asynchronous);
- /*
- PollTCPDriver(&thePB);
- */
- }
- exit(status);
- }
-
- /*
- *
- * perror_MacSocket - substitute for void perror(char *s)
- *
- */
- void perror_MacSocket(char *s)
- {
- char theErrorMsg[128];
- #ifdef MAC_APPL
- char errNoStr[32];
- #endif MAC_APPL
-
- strcpy(theErrorMsg, s);
- strcat(theErrorMsg, ": ");
- strcat(theErrorMsg, strerror_MacSocket(errno_MacSocket));
- #ifndef MAC_APPL
- fprintf(stderr, "%s (%d).\n", theErrorMsg, errno_MacSocket);
- #else MAC_APPL
- NumToString(errno_MacSocket, errNoStr);
- DoAlert(stopIcon, CtoPstr(theErrorMsg), "\p (", errNoStr, "\p).");
- #endif MAC_APPL
- }
-
- /*
- *
- * strerror_MacSocket - substitute for strerror(int errnum)
- *
- */
- char* strerror_MacSocket(int errnum)
- {
- char *errStrPtr;
-
- switch (errnum)
- {
- case noTCPDriver:
- errStrPtr = "UDP-TCP/IP driver (\".IPP\") not available";
- break;
- case invalidArgument:
- errStrPtr = "invalid address argument";
- break;
- case nonSOCK_STREAM:
- errStrPtr = "socket type not supported";
- break;
- case nonSocket:
- errStrPtr = "invalid socket descriptor";
- break;
- case nonAF_INET:
- errStrPtr = "domain not supported";
- break;
- case noBufSpace:
- errStrPtr = "no memory for buffers available";
- break;
- case ipBadLapErr:
- errStrPtr = "bad network configuration";
- break;
- case ipBadCnfgErr:
- errStrPtr = "bad IP configuration error";
- break;
- case ipNoCnfgErr:
- errStrPtr = "missing IP or LAP configuration error";
- break;
- case ipLoadErr:
- errStrPtr = "error in MacTCP load";
- break;
- case ipBadAddr:
- errStrPtr = "error in getting address";
- break;
- case streamAlreadyOpen:
- errStrPtr = "an open stream is already using this receive buffer area";
- break;
- case invalidLength:
- errStrPtr = "the buffer is too small or too large";
- break;
- case invalidBufPtr:
- errStrPtr = "illegal buffer pointer";
- break;
- case insufficientResources:
- errStrPtr = "there are already 64 TCP streams open";
- break;
- case invalidStreamPtr:
- errStrPtr = "the specified TCP stream is not open";
- break;
- case connectionExists:
- errStrPtr = "this TCP stream has an open connection";
- break;
- case duplicateSocket:
- errStrPtr = "a connection already exists on this link";
- break;
- case commandTimeout:
- errStrPtr = "no connection attempt was received in the specified time-out period";
- break;
- case openFailed:
- errStrPtr = "the connection came halfway up and then failed";
- break;
- case connectionDoesntExist:
- errStrPtr = "there is no open connection on this stream";
- break;
- case connectionClosing:
- errStrPtr = "a TCP close command was already issued";
- break;
- case connectionTerminated:
- errStrPtr = "the connection went down";
- break;
- case invalidRDS:
- errStrPtr = "trying to release wrong buffer";
- break;
- default:
- errStrPtr = strerror(errnum);;
- break;
- }
- strcpy(macSocketErrorMssg, errStrPtr);
- return(macSocketErrorMssg);
- }
-
-
- /**
- **
- ** MacSocket calls
- **
- **/
-
- /*
- *
- * accept - accept a connection on a socket
- *
- */
- /*
- int accept(int s, struct sockaddr *addr, int addrlen)
- {
- };
- */
-
- /*
- *
- * bind - bind a name to a socket
- *
- */
- /*
- int bind(int s, struct sockaddr *name, int namelen)
- {
- };
- */
-
- /*
- *
- * connect - initiate a connection on a socket
- *
- */
- int connect(int s, struct sockaddr_in *name, int namelen)
- {
- TCPiopb *theTCPPtr;
- SockElmntPtr theSockPtr;
-
- if (namelen != sizeof(struct sockaddr_in))
- {
- errno_MacSocket = invalidArgument;
- return (-1);
- }
-
- if ((theSockPtr = FindSocketStruct(s)) == NIL)
- {
- errno_MacSocket = invalidStreamPtr;
- return (-1);
- }
-
- theTCPPtr = &(theSockPtr->thePB);
- theTCPPtr->csCode = TCPActiveOpen;
- theTCPPtr->csParam.open.ulpTimeoutValue = ConnectTimeout;
- theTCPPtr->csParam.open.ulpTimeoutAction = Abort;
- theTCPPtr->csParam.open.validityFlags =
- (byte)(timeoutValue | timeoutAction | typeOfService | precedence);
- memcpy((void *)&theTCPPtr->csParam.open.remoteHost, (void *)&name->sin_addr,
- sizeof(name->sin_addr));
- theTCPPtr->csParam.open.remotePort = name->sin_port;
- theTCPPtr->csParam.open.localPort = 0;
- theTCPPtr->csParam.open.tosFlags = (byte)(reliability | lowDelay);
- theTCPPtr->csParam.open.precedence = 0;
- theTCPPtr->csParam.open.dontFrag = False;
- theTCPPtr->csParam.open.timeToLive = (byte)60;
- theTCPPtr->csParam.open.security = False;
- theTCPPtr->csParam.open.optionCnt = (byte)0;
- theTCPPtr->csParam.open.userDataPtr = (Ptr)&theUserData;
- PBControl((ParmBlkPtr)theTCPPtr, Asynchronous);
- PollTCPDriver(theTCPPtr);
- if (errno_MacSocket == noErr)
- {
- theSockPtr->state |= Connected;
- return (0);
- }
- else
- {
- theSockPtr->state &= ~Connected;
- return (-1);
- }
- };
-
- /*
- *
- * listen - listen for connections on a socket
- *
- */
- /*
- int listen(int s, int backlog)
- {
- };
- */
-
- /*
- *
- * recv - receive a message from a socket
- *
- */
- int recv(int s, char *msg, int len, int flags)
- {
- TCPiopb *theTCPPtr;
- SockElmntPtr theSockPtr;
-
- if ((theSockPtr = FindSocketStruct(s)) == NIL)
- {
- errno_MacSocket = invalidStreamPtr;
- return (-1);
- }
-
- theTCPPtr = &(theSockPtr->thePB);
- theTCPPtr->csCode = TCPRcv;
- theTCPPtr->csParam.receive.commandTimeoutValue = RecvTimeout;
- theTCPPtr->csParam.receive.rcvBuff = msg;
- theTCPPtr->csParam.receive.rcvBuffLen = len;
- theTCPPtr->csParam.receive.userDataPtr = (Ptr)&theUserData;
- PBControl((ParmBlkPtr)theTCPPtr, Asynchronous);
- PollTCPDriver(theTCPPtr);
- if (errno_MacSocket == noErr)
- {
- return (theTCPPtr->csParam.receive.rcvBuffLen);
- }
- else
- {
- return (-1);
- }
- };
-
- /*
- *
- * recvfrom - receive a message from a socket
- *
- */
- int recvfrom(int s, char *msg, int len, int flags, struct sockaddr_in *from, int *fromlen)
- {
- SockElmntPtr theSockPtr;
-
- if ((theSockPtr = FindSocketStruct(s)) == NIL)
- {
- errno_MacSocket = invalidStreamPtr;
- return (-1);
- }
-
- if (from != NULL)
- {
- *fromlen = sizeof(from);
- if (!theSockPtr->state & Connected)
- {
- if (!connect(s, from, *fromlen))
- {
- return(-1);
- }
- }
- }
- return(recv(s, msg, len, flags));
- };
-
- /*
- *
- * recvmsg - receive a message from a socket
- *
- */
- int recvmsg(int s, struct msghdr *msg, int flags)
- {
- SockElmntPtr theSockPtr;
-
- if ((theSockPtr = FindSocketStruct(s)) == NIL)
- {
- errno_MacSocket = invalidStreamPtr;
- return (-1);
- }
-
- if (msg->msg_name != NULL)
- {
- if (!theSockPtr->state & Connected)
- {
- if (!connect(s, (struct sockaddr_in *)msg->msg_name, msg->msg_namelen))
- {
- return(-1);
- }
- }
- }
- return(recv(s, (char *)msg->msg_iov, msg->msg_iovlen, flags));
- };
-
- /*
- *
- * send - send a message to a socket
- *
- */
- int send(int s, char *msg, int len, int flags)
- {
- TCPiopb *theTCPPtr;
- SockElmntPtr theSockPtr;
-
- struct theWDS
- {
- short length;
- Ptr theDataPtr;
- short end;
- } theWDS;
-
-
- if ((theSockPtr = FindSocketStruct(s)) == NIL)
- {
- return (-1);
- }
- theTCPPtr = &(theSockPtr->thePB);
- theTCPPtr->csCode = TCPSend;
- theTCPPtr->csParam.send.ulpTimeoutValue = SendTimeout;
- theTCPPtr->csParam.send.ulpTimeoutAction = True;
- theTCPPtr->csParam.send.validityFlags = (byte)(timeoutValue | timeoutAction);
- theTCPPtr->csParam.send.pushFlag = True; /* May be false ?? */
- theTCPPtr->csParam.send.urgentFlag = False;
- theWDS.length = len;
- theWDS.theDataPtr = msg;
- theWDS.end = 0;
- theTCPPtr->csParam.send.wdsPtr = (Ptr)&theWDS;
- theTCPPtr->csParam.send.userDataPtr = (Ptr)&theUserData;
- PBControl((ParmBlkPtr)theTCPPtr, Asynchronous);
- PollTCPDriver(theTCPPtr);
- if (errno_MacSocket == noErr)
- {
- return(len);
- }
- else
- {
- return (-1);
- }
- };
-
- /*
- *
- * sendto - send a message to a socket
- *
- */
- int sendto(int s, char *msg, int len, int flags, struct sockaddr_in *to, int tolen)
- {
- SockElmntPtr theSockPtr;
-
- if ((theSockPtr = FindSocketStruct(s)) == NIL)
- {
- errno_MacSocket = invalidStreamPtr;
- return (-1);
- }
-
- if (to != NULL)
- {
- if (!theSockPtr->state & Connected)
- {
- if (!connect(s, to, tolen))
- {
- return(-1);
- }
- }
- }
- return(send(s, msg, len, flags));
- };
-
- /*
- *
- * sendmsg - send a message to a socket
- *
- */
- int sendmsg(int s, struct msghdr *msg, int flags)
- {
- SockElmntPtr theSockPtr;
-
- if ((theSockPtr = FindSocketStruct(s)) == NIL)
- {
- errno_MacSocket = invalidStreamPtr;
- return (-1);
- }
-
- if (msg->msg_name != NULL)
- {
- if (!theSockPtr->state & Connected)
- {
- if (!connect(s, (struct sockaddr_in *)msg->msg_name, msg->msg_namelen))
- {
- return(-1);
- }
- }
- }
- return(send(s, (char *)msg->msg_iov, msg->msg_iovlen, flags));
- };
-
-
- #ifdef MAC_APPL
- /*
- *
- * SetDialogShowTime - sets the time that an MacTCP/IP info DialogBox is vissible.
- * when sec <= 0, no message will be displayed.
- *
- */
- void SetDialogShowTime(int sec)
- {
- showTime = sec;
- }
- #endif MAC_APPL
-
- /*
- *
- * shutdown - shut down part of a full-duplex connection
- *
- */
- int shutdown(int s, int how)
- {
- TCPiopb *theTCPPtr;
- SockElmntPtr theSockPtr;
-
-
- switch (how)
- {
- case 1: /* further sends disalowed */
- return (close_MacSocket(s));
- break;
- case 2: /* further sends and receives disalowed */
- if ((theSockPtr = FindSocketStruct(s)) == NIL)
- {
- return(-1);
- }
- theTCPPtr = &(theSockPtr->thePB);
- theTCPPtr->csCode = TCPRelease;
- PBControl((ParmBlkPtr)theTCPPtr, Asynchronous);
- PollTCPDriver(theTCPPtr);
- return (RemoveSocketStruct(s));
- break;
- case 0: /* further receives disalowed */
- default:
- errno_MacSocket = invalidArgument;
- return(-1);
- break;
- }
- };
-
- /*
- *
- * socket - create an endpoint for communication
- *
- */
- int socket(int domain, int type, int protocol)
- {
- TCPiopb *theTCPPtr;
- SockElmntPtr theSockPtr;
-
- if (domain != AF_INET)
- {
- errno_MacSocket = nonAF_INET;
- return (-1);
- }
- if (protocol != 0)
- {
- errno_MacSocket = invalidArgument;
- return (-1);
- }
- if (type != SOCK_STREAM)
- {
- errno_MacSocket = nonSOCK_STREAM;
- return (-1);
- }
- if ((theSockPtr = (SockElmntPtr)CreateSocketStruct()) == NIL)
- {
- errno_MacSocket = noBufSpace;
- return (-1);
- }
-
- if (TCPDriverNotOpened)
- {
- if (!OpenTCPDriver())
- {
- return(-1);;
- }
- }
- theTCPPtr = &(theSockPtr->thePB);
- theTCPPtr->ioCompletion = NIL;
- theTCPPtr->ioRefNum = ioRefNumTCP;
- theTCPPtr->csCode = TCPCreate;
- theTCPPtr->csParam.create.rcvBuff = theSockPtr->sockBuf;
- theTCPPtr->csParam.create.rcvBuffLen = SockBufSize;
- theTCPPtr->csParam.create.notifyProc = SockNotify;
- theTCPPtr->csParam.create.userDataPtr = (Ptr)&theUserData;
- PBControl((ParmBlkPtr)theTCPPtr, Asynchronous);
- PollTCPDriver(theTCPPtr);
- if (errno_MacSocket == noErr)
- {
- return (theSockPtr->number);
- }
- else
- {
- RemoveSocketStruct(theSockPtr->number);
- return (-1);
- };
- }
-
-